package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Credit;
import com.ethan.cj.ssm.entity.Documenttype;
import com.ethan.cj.ssm.service.CreditService;
import com.ethan.cj.ssm.service.DocumenttypeService;
import com.ethan.cj.ssm.util.JsonUtil;
import lombok.extern.log4j.Log4j;
import org.json.JSONObject;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import java.util.Map;

@Controller
@Log4j
@RequestMapping("credit")
public class CreditController {


    @Autowired
    private CreditService creditService;


    @Autowired
    private DocumenttypeService documenttypeService;

    /**
     * 进入全部贷款页面
     *
     * @return
     */
    @RequestMapping("/creditlist")
    public String creditlist() {
        return "creditlist";
    }

    /**
     * 全部贷款请求地址
     *
     * @return 路径
     */
    @RequestMapping("/getCreditList")
    @ResponseBody
    public Map<Object, Object> getCreditList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("name") String paramString) {
        Map map = JsonUtil.parseObject(paramString, Map.class);
        return creditService.getCreditList(page, limit, map);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<Documenttype> list = documenttypeService.list();
        model.addAttribute("list", list);
        return "addCredit";
    }

    @RequestMapping("addCredit")
    public String addCredit() {
        return null;
    }

    @RequestMapping("selectCreditCid")
    public String selectCreditCid(@RequestParam("cid") int cid, @RequestParam("cstate") int cstate, Model model) {
//        if (cstate == 1) {
            Credit credit = creditService.selectCreditCid(cid, cstate);
            List<Documenttype> list = documenttypeService.list();
            model.addAttribute("list", list);
            model.addAttribute("c", credit);
            return "allocated";
//        } else if (cstate == 2) {
//
//        } else if (cstate == 3) {
//
//        } else if (cstate == 4) {
//
//        } else if (cstate == 5) {
//
//        } else {
//            return null;
//        }
//        return null;
    }

}
